const userinfo = require("../database/userinfo");
const uuid = require("uuid");
const md5 = require("md5");

module.exports = async (req, res)=>{
  const {tel} = req.body;

  if( !tel ){
    res.send({
      code:0,
      msg:"缺少必传参数：tel",
      data:null
    })
    return;
  }

  try{
    const userData = await userinfo.find({ tel });
    if(userData.length < 1){
      await userinfo.insertMany({
        userId: "user-" + uuid.v1(),
        tel,
        nickName: "",
        password: md5(tel.slice(-6)),
        birth: 0,
        email: "",
        registerTime: Date.now(),
        loginTime: Date.now(),
        power: 0,   // 0用户，1管理员
        avatar: ""
      });
      res.send({
        code:1,
        msg:"注册成功",
        data:null
      })
    }else{
      res.send({
        code:3,
        msg:"手机号已存在",
        data:null
      })
    }
  }catch(err){
    res.send({
      code: 2,
      msg: "数据库操作出错",
      data: err.toString()
    })
  }
}